# Maintainer: Sven-Hendrik Haase <svenstaro@archlinux.org>
# Contributor: Lubomir 'Kuci' Kucera <kuci24-at-gmail-dot-com>

pkgname=gitlab-runner
pkgver=15.11.0
pkgrel=1
pkgdesc="The official GitLab CI runner written in Go"
arch=('loong64' 'x86_64')
url='https://gitlab.com/gitlab-org/gitlab-runner'
license=('GPL3')
depends=('ca-certificates' 'curl' 'git' 'glibc' 'tar')
makedepends=('git' 'go' 'git' 'mercurial' 'gox')
install=gitlab-runner.install
replaces=('gitlab-ci-multi-runner')
backup=('etc/gitlab-runner/config.toml')
noextract=("prebuilt-alpine-arm-${pkgver}.tar.xz"
           "prebuilt-alpine-arm64-${pkgver}.tar.xz"
           "prebuilt-alpine-s390x-${pkgver}.tar.xz"
           "prebuilt-alpine-x86_64-pwsh-${pkgver}.tar.xz"
           "prebuilt-alpine-x86_64-${pkgver}.tar.xz"
           "prebuilt-ubuntu-arm-${pkgver}.tar.xz"
           "prebuilt-ubuntu-arm64-${pkgver}.tar.xz"
           "prebuilt-ubuntu-s390x-${pkgver}.tar.xz"
           "prebuilt-ubuntu-x86_64-pwsh-${pkgver}.tar.xz"
           "prebuilt-ubuntu-x86_64-${pkgver}.tar.xz")
source=("git+https://gitlab.com/gitlab-org/gitlab-runner.git#tag=v${pkgver}"
        "prebuilt-alpine-arm-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-alpine-arm.tar.xz"
        "prebuilt-alpine-arm64-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-alpine-arm64.tar.xz"
        "prebuilt-alpine-s390x-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-alpine-s390x.tar.xz"
        "prebuilt-alpine-x86_64-pwsh-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-alpine-x86_64-pwsh.tar.xz"
        "prebuilt-alpine-x86_64-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-alpine-x86_64.tar.xz"
        "prebuilt-ubuntu-arm-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-ubuntu-arm.tar.xz"
        "prebuilt-ubuntu-arm64-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-ubuntu-arm64.tar.xz"
        "prebuilt-ubuntu-s390x-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-ubuntu-s390x.tar.xz"
        "prebuilt-ubuntu-x86_64-pwsh-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-ubuntu-x86_64-pwsh.tar.xz"
        "prebuilt-ubuntu-x86_64-${pkgver}.tar.xz::https://gitlab-runner-downloads.s3.amazonaws.com/v${pkgver}/helper-images/prebuilt-ubuntu-x86_64.tar.xz"
        "gitlab-runner.service"
        "gitlab-runner.sysusers"
        "gitlab-runner.tmpfiles"
        "config.toml")
sha512sums=('SKIP'
            '6a2131456879fd82aa60eba01f2f62329932bd212fb577efb193d4b9962e07dd1a02059d193257b36fe4811ea825cbb58a23fa96a07e7f5bcb4e260abfbfe1fc'
            '2c96e21d1425b394742761ef25b782676c0ca05a4737fff879d52522eb4f4ef76e53a9f06a05c2f34b8188c39d18373fa72023fd964635e5462db5cd964432b0'
            '0036c3419e22e5231523464dfc9faa9037efc6daf28b57235d669e34f403836660f23276be89b37c9228251b32e228d5287bdfa861dbb2dd1dd9a6e1f2f874f9'
            '3f9d80020eb211e4b7c3943a7a46316bcf0bb26bf306c7acd883b854d102b23429221b6f78a6d1b81b1867769731f53f029633c20dd696450a626d8cca5db34f'
            'b16a723ffaed2dc43c2a3907eecc06740c501022e2a7cc7f23000675015ca7ca856f10c986a30f35f79b2df70c17c414d5b2d4b04853daaf5f3ab16b3d07e314'
            '004444ef561251e996067a6ff2c9a94e4c61cda8616e460a246b8fd85c9961e35f39fd61f18edf242761ec3a60012897b35ddcff0c194b24deb29c517d851188'
            '698a9db57b86008faf5a7582887df4890ee691b6e5f477cd889d5a5c716bee0b965dfb255271428fea045c8f707362b4ce238f9b9f26de2a22f4397ef541db86'
            '048fdd9d71d73ebf76ccb0a3b1bc84bd6b810c00f4bdbf9fa76dc12c76f2d9cf31b653d305b3b78cb199d80346c65d3e2c49fb30d52a158954ef59dbdecad434'
            '3e088bbb4297799a4b3124ced780bbc494f3aeee16d666b20679581708ebfd8fb51b452ae249f533395bb14b760fe18fcaf905ee6542ca8c4a41bca53d218153'
            '781461e7c1a96db55164b2460ad0584f2b3846c1be58021ebc433f737bd53ddaa4de88008d5ae460d136c9d4dcdf73e04596cae788cd964125a81fa8fead8432'
            'c0af374b9986895aedcfaee6c67cfad68f0f7289f87e4611358adaff59a2f349f55764fe28b2b1f61f8bfeb61126d4f90d433c626fdf9b826a2de6217f86574f'
            '8aa7f08702e99053c696fcc2aaba83beb9e9cd6f31973d82862db9350ac46df3a095377625d31fe909677525290d2de922d7a97930ed235774cb8f0da8944d40'
            '6751d9fa0b27172d1b419c4138f5ac15cbc7c9147653a7258cf1470216142c637210bb60608c7ed0974e0e4057e5ddeae32225df1bb36e7dd1f20fec71e33cc3'
            '9718b94bd0ddb09095ffb8c1e60ca1e9649dabb1747e7fc95e58e404b2f9effdeb4cfd759f5b904443dc53a4e18c02003c38f85584713deb49f6a6d1007503de')

prepare() {
  cd gitlab-runner

  local version=$(make version | grep "Current version:" | sed -n "s/.*: \(.*\)/\1/p")
  local revision=$(make version | grep "Current revision:" | sed -n "s/.*: \(.*\)/\1/p")
  local branch=$(make version | grep "Current branch:" | sed -n "s/.*: \(.*\)/\1/p")

  sed -i "s/var VERSION.*/var VERSION = \"$version\"/" common/version.go
  sed -i "s/var REVISION.*/var REVISION = \"$revision\"/" common/version.go
  sed -i "s/var BRANCH.*/var BRANCH = \"$branch\"/" common/version.go
}

build() {
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export GOFLAGS="-buildmode=pie -trimpath -ldflags=-linkmode=external -mod=readonly -modcacherw"

  cd gitlab-runner
  go build -o gitlab-runner .
}

package() {
  cd gitlab-runner

  install -Dm644 "${srcdir}/config.toml" "${pkgdir}/etc/gitlab-runner/config.toml"
  install -Dm644 "${srcdir}/gitlab-runner.service" "${pkgdir}/usr/lib/systemd/system/gitlab-runner.service"
  install -Dm644 "${srcdir}/gitlab-runner.sysusers" "${pkgdir}/usr/lib/sysusers.d/gitlab-runner.conf"
  install -Dm644 "${srcdir}/gitlab-runner.tmpfiles" "${pkgdir}/usr/lib/tmpfiles.d/gitlab-runner.conf"
  install -Dm755 gitlab-runner "${pkgdir}/usr/bin/gitlab-runner"
  install -Dm755 packaging/root/usr/share/gitlab-runner/clear-docker-cache "${pkgdir}/usr/share/gitlab-runner/clear-docker-cache"

  # Move prebuilt Docker images to hard-coded canonical location
  for image in prebuilt-{alpine,ubuntu}-{arm,arm64,s390x,x86_64-pwsh,x86_64}-${pkgver}.tar.xz; do
    install -Dm644 "${srcdir}/${image}" "${pkgdir}/usr/lib/gitlab-runner/helper-images/${image}"
  done
}
